/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * 
 *         http://www.apache.org/licenses/LICENSE-2.0
 *         
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. Unless required by applicable law or
 * agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */
package org.rhok.ncn.background;

import java.io.IOException;
import java.util.Arrays;

import org.rhok.ncn.common.CommonData;
import org.rhok.ncn.communicator.WebClient;
import org.rhok.ncn.datatypes.LocationData;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;

/**
 * Listener to track the phone's current GPS location and update a text view with that information
 * as it changes.
 * @author pfritzsche
 */
public final class TrackingLocationListener implements LocationListener {
	private static final String TAG = "TrackingLocationListener";
    
	/**
	 * Called when the phone's GPS location has changed. Updates the GPS text view with the current
	 * latitude and longitude, adds the location to the queue of shared locations for the map
	 * overlay system, and adds the information to the database for permanent storage.
	 * @param location object containing all new location information
	 */
	public void onLocationChanged(Location location) {
		LocationData data = new LocationData(location.getLatitude(), location.getLongitude(),
				CommonData.signalStrength, CommonData.wifiStrength);
		CommonData.db().addRowAndClose(data);
		
		try {
            WebClient.submitData(data);
        } catch (IOException e) {
            Log.e(TAG, "Error submitting data to server: " + e.getMessage());
        }
		
		if (CommonData.isActivityRunning) {
		    Message msg = CommonData.handler().obtainMessage();
		    msg.obj = Arrays.asList(data);
		    CommonData.handler().sendMessage(msg);
		}
	}

	/**
	 * Required method to implement the interface. We don't care about what happens when the
	 * provider has been disabled, so this is left doing nothing.
	 */
	public void onProviderDisabled(String provider) { }
	
	/**
	 * Required method to implement the interface. We don't care about what happens when the
	 * provider has been enabled, so this is left doing nothing.
	 */
	public void onProviderEnabled(String provider) { }
	
	/**
	 * Required method to implement the interface. We don't care about what happens when any
	 * non-location statuses have changed, so this is left doing nothing.
	 */
	public void onStatusChanged(String provider, int status, Bundle extras) { }

}
